/*
 *所有关于Workforce类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*WorkforceService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VoL.Domain.IRepositories;
using VoL.Domain.Infrastructure;
using VOL.Core.ManageUser;
using VoL.Domain.Dtos.Workforce;

namespace VoL.Domain.Services
{
    public partial class WorkforceService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IBaseRepository<Workforce> _workforcrepository;
        private readonly IBaseRepository<Nurse> _nurseRepository;
        private readonly IWorkforceRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public WorkforceService(
            IWorkforceRepository dbRepository,
            IHttpContextAccessor httpContextAccessor,
            IBaseRepository<Workforce> workforcrepository,
            IBaseRepository<Nurse> nurseRepository
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _workforcrepository = workforcrepository;
            _nurseRepository = nurseRepository;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        public async Task<WebResponseContent> AddWorkforeces(List<Workforce> list)
        {
            var response = new WebResponseContent();
            var lastDay = list.Last().Stage;
            var tempEntity = _workforcrepository.GetAll().Where(o => o.NurseCode == list[0].NurseCode && o.Year == list[0].Year && o.Month == list[0].Month && o.Stage == lastDay);
            if (tempEntity.Any())
            {
                response.Status = false;
                response.Message = "添加失败,当前阶段已排班";
                return await Task.FromResult(response);
            }
            //  var creator = UserContext.Current.UserName;
            var creator = "111";
            var time = DateTime.Now;
            list.ForEach(o =>
            {
                o.Creator = creator;
                o.CreateTime = time;
            });
            int c = await _workforcrepository.AddRange(list);
            if (c > 0)
            {
                response.OK("添加成功", c);
            }
            response.OK("添加成功");
            return await Task.FromResult(response);
        }

        public async Task<WebResponseContent> GetNurseList()
        {
            var response = new WebResponseContent();
            var list = await _nurseRepository.GetAll().ToListAsync();
            response.OK("查询成功", list);
            return await Task.FromResult(response);
        }


        public async Task<WebResponseContent> GetWorkforces(int year, int month, int stage)
        {
            var response = new WebResponseContent();
            var list = _workforcrepository.GetAll().Where(o => o.Year == year && o.Month == month);
            if (stage == 1)
            {
                list = list.Where(o => Convert.ToInt32(o.Stage) <= 15);
            }
            else
            {
                list = list.Where(o => Convert.ToInt32(o.Stage) > 15);
            }
            response.OK("查询成功", list);
            return await Task.FromResult(response);
        }


        public async Task<WebResponseContent> GetWorkforcesDetail(GetWorkforceDetailDto obj)
        {
            var list = _workforcrepository.GetAll()
                .Where(o => o.NurseCode == obj.nurseCode && o.Year == obj.year && o.Month == obj.month);

            if (obj.stage == 1)
            {
                list = list.Where(o => Convert.ToInt32(o.Stage) <= 15);
            }
            else
            {
                list = list.Where(o => Convert.ToInt32(o.Stage) > 15);
            }
            var response = new WebResponseContent();
            response.OK("查询成功", list);
            return await Task.FromResult(response);
        }


        public async Task<WebResponseContent> UpdWorkforeces(List<Workforce> list)
        {
            var response = new WebResponseContent();
            var creator = UserContext.Current.UserName;
            var time = DateTime.Now;
            list.ForEach(o =>
            {
                o.Creator = creator;
                o.CreateTime = time;
            });
            int c = await _workforcrepository.UpdateRange(list);
            if (c > 0)
            {
                response.OK("修改成功", c);
            }
            return await Task.FromResult(response);
        }
    }
}
